//
//  VCMenuViewController.m
//  VideoCollage
//
//  Created by Mac on 2018/6/21.
//  Copyright © 2018年 stoneobs.qq.com. All rights reserved.
//

#import "VCMenuViewController.h"
#import "NSBundle+STSystemTool.h"
#import "VCItemEditViewController.h"
#import "VCSettingViewController.h"
@interface VCMenuViewController ()
@property(nonatomic, strong) NSMutableArray                     *buttonArray;/**< <##> */
@property(nonatomic, strong) NSMutableArray                     *scrollButtonArray;/**< <##> */
@property(nonatomic, strong) UIScrollView                     *scrollView;/**< <##> */
@end

@implementation VCMenuViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
    [self st_setLeftItemWithTitle:NSBundle.st_applictionDisplayName titleColor:FirstTextColor];
    [self st_setRightItemWithImage:[UIImage imageNamed:@"set"]];
    
    
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
//    self.view.backgroundColor = [UIColor colorWithGradientStyle:UIGradientStyleTopToBottom withFrame:UIScreenFrame andColors:@[FlatOrange,FlatCoffee,FlatMint]];
    self.view.backgroundColor = UIColor.whiteColor;
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, TMUtils.tabBarTop + 49 - TMUtils.navgationBarBootom)];
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.scrollEnabled = NO;
    header.backgroundColor = UIColor.clearColor;
    
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 156)];
    bannerImageView.image = [UIImage imageNamed:@"home-bottom"];
    bannerImageView.bottom = header.height;
    [header addSubview:bannerImageView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(20, 0, UIScreenWidth, 30)
                                                     text:@"● Recommended"
                                                textColor:FirstTextColor
                                                     font:16
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    titleLable.bottom = bannerImageView.top;
    titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:@"●" attributes:@{NSForegroundColorAttributeName:FlatOrange}];
    [header addSubview:titleLable];
    

    self.buttonArray = NSMutableArray.new;
    self.scrollButtonArray = NSMutableArray.new;
    CGFloat  leftInset = 20;
    CGFloat  itemInset = 15;
    NSInteger rowCount = 5;
    
    CGFloat scrollItemWith = 100;//底部滑动高度
    CGFloat itemWith = (UIScreenWidth - leftInset * 2 - rowCount*itemInset + itemInset) / rowCount;
    
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, scrollItemWith)];
    self.scrollView = scrollView;
    scrollView.backgroundColor = UIColor.clearColor;
    if (ios11 && [scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        [scrollView setContentInsetAdjustmentBehavior:@(2)];
    }
    scrollView.bottom = header.height - 30;
    scrollView.showsHorizontalScrollIndicator = NO;
    [header addSubview:scrollView];
    __weak typeof(self) weakSelf =  self;
    for (NSInteger i =0 ; i < 20; i ++ ) {
        
        UIImage * image = [UIImage imageNamed:@(i+1).description];
        STButton * itemButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, itemWith, itemWith)
                                                         title:@(i).description
                                                    titleColor:nil
                                                      titleFont:0
                                                  cornerRadius:4
                                               backgroundColor:nil
                                               backgroundImage:image
                                                         image:nil];
        [itemButton setBackgroundImage:image forState:UIControlStateHighlighted];
        [itemButton setClicAction:^(UIButton *sender) {
            [weakSelf onSlectedButtonWithRank:(i+1) sender:sender];
        }];
        itemButton.left = leftInset + itemWith * (i%rowCount) + itemInset * (i%rowCount) ;
        itemButton.top = itemInset + itemWith *  (i/rowCount) + itemInset * (i/rowCount);
        [header addSubview:itemButton];
        [self.buttonArray addObject:itemButton];
        
        STButton * scrollButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, scrollItemWith, scrollItemWith)
                                                          title:@(i).description
                                                     titleColor:nil
                                                      titleFont:0
                                                   cornerRadius:4
                                                backgroundColor:nil
                                                backgroundImage:image
                                                          image:nil];
        [scrollButton setBackgroundImage:image forState:UIControlStateHighlighted];
        [scrollButton setClicAction:^(UIButton *sender) {
            [weakSelf onSlectedScrolllButtonWithRank:(i+1) sender:sender];
        }];
        scrollButton.left = leftInset + scrollItemWith * i + itemInset * i ;
        [scrollView addSubview:scrollButton];
        scrollView.contentSize = CGSizeMake(scrollButton.right +leftInset , 0);
        [self.scrollButtonArray addObject:scrollButton];
        
    }
    self.tableView.tableHeaderView = header;
    
    
}

#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{

    [self.navigationController pushViewController:VCSettingViewController.new animated:YES];
}
- (void)onSlectedButtonWithRank:(NSInteger)rank sender:(UIButton*)sender{
    for (UIButton * button in self.buttonArray) {
        button.selected = NO;
        sender.selected = YES;
        if (button.selected) {
            [button st_setBorderWith:1 borderColor:TM_redColor cornerRadius:4];
        }else{
            [button st_setBorderWith:1 borderColor:UIColor.clearColor cornerRadius:4];
        }
    }
    
    NSInteger index = rank - 1;
    UIButton * selectedButton = self.scrollButtonArray[index];
    for (NSInteger i = 0; i < self.scrollButtonArray.count; i ++) {
        UIButton * button = self.scrollButtonArray[i];
        button.selected = NO;
        selectedButton.selected = YES;
        if (button.selected) {
            [button st_setBorderWith:1 borderColor:TM_redColor cornerRadius:4];
        }else{
            [button st_setBorderWith:1 borderColor:UIColor.clearColor cornerRadius:4];
        }
    }
    CGFloat left = selectedButton.right - self.scrollView.width  ;
    left = left < 0?0:left;
    [self.scrollView setContentOffset:CGPointMake(left, 0) animated:YES];
    
    [self gotoEditVCWithRank:rank];
}
- (void)onSlectedScrolllButtonWithRank:(NSInteger)rank sender:(UIButton*)sender{
    
    for (UIButton * button in self.scrollButtonArray) {
        button.selected = NO;
        sender.selected = YES;
        if (button.selected) {
            [button st_setBorderWith:1 borderColor:TM_redColor cornerRadius:4];
        }else{
            [button st_setBorderWith:1 borderColor:UIColor.clearColor cornerRadius:4];
        }
    }
    
    NSInteger index = rank - 1;
    for (NSInteger i = 0; i < self.buttonArray.count; i ++) {
        UIButton * button = self.buttonArray[i];
        UIButton * selectedButton = self.buttonArray[index];
        button.selected = NO;
        selectedButton.selected = YES;
        if (button.selected) {
            [button st_setBorderWith:1 borderColor:TM_redColor cornerRadius:4];
        }else{
            [button st_setBorderWith:1 borderColor:UIColor.clearColor cornerRadius:4];
        }
    }
    [self gotoEditVCWithRank:rank];
}
- (void)gotoEditVCWithRank:(NSInteger)rank{
    DDLogInfo(@"点击了第%ld 个item",rank);
    VCItemEditViewController *vc = VCItemEditViewController.new;
    vc.rank = rank;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
